package com.lvmama.task.back.sweb;

import com.lvmama.comm.pet.po.perm.PermUser;
import com.lvmama.comm.utils.ServletUtil;
import com.lvmama.task.back.utils.Pagination;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.lvmama.comm.vo.perm.Constant.SESSION_BACK_USER;


@Results({
        @Result(name = "login", type = "redirect", location = "login.do")
})
public abstract class BaseAction extends ActionSupport {

    /**
     *
     */
    private static final long serialVersionUID = 9197369313409325055L;

    protected Log LOG = LogFactory.getLog(this.getClass());

    /**
     * Pagination
     */
    protected Pagination pagination;


    public String getBasePath() {
        String basePath = this.getRequest().getContextPath() + "/";
        return basePath;
    }

    /**
     * 初始化当前页和Pagination对象
     */
    protected Pagination initPagination() {
        // request url method
        String currentPage = getRequest().getParameter("page");
        String perPageRecord = getRequest().getParameter("perPageRecord");
        int page = 1;
        if (currentPage != null && !"".equals(currentPage)) {
            page = Integer.parseInt(currentPage);
        }
        // 构建新的Pagination对象
        pagination = new Pagination();
        int iperPageRecord = pagination.getPerPageRecord();
        if (perPageRecord != null && !"".equals(perPageRecord)) {
            iperPageRecord = Integer.parseInt(perPageRecord);
        }
        if (iperPageRecord != pagination.getPerPageRecord()) {
            pagination.setPerPageRecord(iperPageRecord);
            pagination.setPage(page);
        } else {
            pagination.setPage(page);
        }
        return pagination;
    }



    public boolean isLogined() {
        PermUser user = this.getSessionUser();
        if (user != null && user.getPermUserPass()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取当前登录用户
     *
     * @return
     */
    protected PermUser getSessionUser() {
        PermUser user = (PermUser) this.getSession(SESSION_BACK_USER);
        return user;
    }

    public String getOperatorName() {
        return getSessionUser().getUserName();
    }



    public HttpServletResponse getResponse() {
        return ServletActionContext.getResponse();
    }



    protected Object getSession(String key) {
        return ServletUtil.getSession(getRequest(), getResponse(), key);
    }


    public HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }

    public Pagination getPagination() {
        return pagination;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }
}
